package com.cain.calendar.dao;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.cain.calendar.bean.Event;
import com.cain.calendar.tool.Constant;
import com.cain.calendar.tool.MyApplication;

import java.util.ArrayList;

/**
 * Created by Cain on 2016/6/26.
 * 数据库操作类
 */
public class CalendarDAO {
    private static SQLiteDatabase db = null;

    /**
     * 打开数据库
     */
    public static void openDatabase() {
        db = MyApplication.getContext().openOrCreateDatabase(Constant.DATABASE_NAME, Activity.MODE_PRIVATE, null);
        if (db != null) {
            System.out.println("Creating database succeed!");
            if (createTableEvent()) {
                System.out.println("Creating table event_table succeed!");
            } else {
                System.out.println("Creating table event_table failed!");
            }
        } else {
            System.out.println("Creating database failed!");
        }
    }

    /**
     * 创建事件表
     * @return
     */
    private static boolean createTableEvent() {
        String sql = "create table if not exists " + Constant.EVENT_TABLE_NAME +
                "(id integer primary key autoincrement,name text,time text,tag text,remark text)";
        try {
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            System.out.println("Error message:\n" + e.toString());
            return false;
        }
    }

    /**
     * 插入事件
     * @param event
     */
    public static void insertEvent(Event event) {
        ContentValues values = new ContentValues();
        values.put("name", event.getName());
        values.put("time", event.getTimeString());
        values.put("tag", event.getTag());
        values.put("remark", event.getRemark());
        db.insert(Constant.EVENT_TABLE_NAME, null, values);
    }

    /**
     * 删除事件
     * @param event
     */
    public static void deleteEvent(Event event) {
        String whereClause = "id=?";
        String[] whereArgs = {String.valueOf(event.getId())};
        db.delete(Constant.EVENT_TABLE_NAME, whereClause, whereArgs);
    }

    /**
     * 清空事件表
     */
    public static void clearEventTable() {
        ArrayList<Event> eventList = getEventList(null, null, null);
        for (int i = 0; i < eventList.size(); ++i) {
            deleteEvent(eventList.get(i));
        }
    }

    /**
     * 更新事件
     * @param event
     */
    public static void updateEvent(Event event) {
        ContentValues values = new ContentValues();
        values.put("name", event.getName());
        values.put("time", event.getTimeString());
        values.put("tag", event.getTag());
        values.put("remark", event.getRemark());
        String whereClause = "id=?";
        String[] whereArgs = {String.valueOf(event.getId())};
        db.update(Constant.EVENT_TABLE_NAME, values, whereClause, whereArgs);
    }

    /**
     * 获取所有事件
     * @return
     */
    public static ArrayList<Event> getEventList(String[] columns, String selection, String[] selectionArgs) {
        Cursor cursor = db.query (Constant.EVENT_TABLE_NAME, columns, selection, selectionArgs, null, null, null);
        ArrayList<Event> eventList = new ArrayList<>();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            String time = cursor.getString(2);
            String tag = cursor.getString(3);
            String remark = cursor.getString(4);
            eventList.add(new Event(id, name, time, tag, remark));
            cursor.moveToNext();
        }
        return eventList;
    }

    /**
     * 获取某个月的事件列表
     * @param year
     * @param month
     * @return
     */
    public static ArrayList<Event> getEventList(int year, int month) {
        String[] columns = { "id", "name", "time", "tag", "remark"};
        String selection = "time like ?";
        String[] selectionArgs = {String.format("%04d-%02d", year, month) + "%"};
        return getEventList(columns, selection, selectionArgs);
    }

    /**
     * 获取某天的事件列表
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static ArrayList<Event> getEventList(int year, int month, int day) {
        String[] columns = { "id", "name", "time", "tag", "remark"};
        String selection = "time like ?";
        String[] selectionArgs = {String.format("%04d-%02d-%02d", year, month, day) + "%"};
        return getEventList(columns, selection, selectionArgs);
    }

    /**
     * 判断当天是否有日程
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static boolean isDayHasEvent(int year, int month, int day) {
        String[] columns = { "id", "name", "time", "tag", "remark"};
        String selection = "time like ?";
        String[] selectionArgs = {String.format("%04d-%02d-%02d", year, month, day) + "%"};
        ArrayList<Event> events = getEventList(columns, selection, selectionArgs);
        if (events.size() == 0)
            return false;
        else
            return true;
    }

    /**
     * 关闭数据库
     */
    public static void closeDatabase() {
        if (db != null) {
            db.close();
        }
    }
}
